﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NutWinFrm
{
    public partial class ChgFileEncodingFrm : Form
    {
        public ChgFileEncodingFrm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 拷贝文件夹
        /// </summary>
        /// <param name="srcdir">如 c:/temp/123</param>
        /// <param name="desdir">如 d:/temp</param>
        public void CopyDirectory(string srcdir, string desdir, bool overwrite)
        {
            string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);

            string desfolderdir = desdir + "\\" + folderName;

            if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
            {
                desfolderdir = desdir + folderName;
            }
            string[] filenames = Directory.GetFileSystemEntries(srcdir);

            foreach (string file in filenames)// 遍历所有的文件和目录
            {
                if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                {

                    string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
                    if (!Directory.Exists(currentdir))
                    {
                        Directory.CreateDirectory(currentdir);
                    }

                    CopyDirectory(file, desfolderdir, overwrite);
                }

                else // 否则直接copy文件
                {
                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);


                    srcfileName = desfolderdir + "\\" + srcfileName;

                    if (file.ToUpper().LastIndexOf(textBox_ext.Text.ToUpper()) == -1)
                    {
                        log("skip:"+ file);
                        continue;

                    }


                    if (!Directory.Exists(desfolderdir))
                    {
                        Directory.CreateDirectory(desfolderdir);
                    }
                    log("write:" + file);
                    Encoding encode = Encoding.GetEncoding(int.Parse(comboBox_src.SelectedValue.ToString()));
                    string srcText = File.ReadAllText(file, encode);
                    Encoding encode2 = Encoding.GetEncoding(int.Parse(comboBox_out.SelectedValue.ToString()));
                    File.WriteAllText(srcfileName, srcText, encode2);


                   
                }
            }//foreach 
        }

        public void log(String value)
        {
           
            richTextBox_rs.AppendText(value + "\n");
        }



       

        private void ChgFileEncodingFrm_Shown(object sender, EventArgs e)
        {

            ///加载编码列表
            EncodingInfo[] ens = Encoding.GetEncodings();
            comboBox_out.DataSource = ens;
            comboBox_out.DisplayMember = "DisplayName";
            comboBox_out.ValueMember = "CodePage";
            comboBox_out.SelectedValue = Encoding.UTF8.CodePage;
            EncodingInfo[] ens2 = Encoding.GetEncodings();
            comboBox_src.DataSource = ens2;
            comboBox_src.DisplayMember = "DisplayName";
            comboBox_src.ValueMember = "CodePage";
            comboBox_src.SelectedValue = 54936;






        }

        private void button_run_Click(object sender, EventArgs e)
        {
            String rs = "";
            rs = comboBox_out.SelectedValue.ToString();
            richTextBox_rs.AppendText(rs + "\n");
            rs = comboBox_src.SelectedValue.ToString();
            richTextBox_rs.AppendText(rs + "\n");
            CopyDirectory(textBox_src.Text, textBox_out.Text, true);

            log("end ");



        }

        private void button_src_dir_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog().Equals(DialogResult.OK))
            {
                textBox_src.Text = folderBrowserDialog.SelectedPath;

            }
        }

        private void button_out_dir_Click(object sender, EventArgs e)
        {
            if (checkBox_same.Checked)
            {
                textBox_out.Text = textBox_src.Text + "_out";
            }
            else if (folderBrowserDialog.ShowDialog().Equals(DialogResult.OK))
            {
                textBox_out.Text = folderBrowserDialog.SelectedPath;

            }
        }
    }
}
